<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="牛舍名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['cowshename']" placeholder="请输入牛舍名称" disabled  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="配方类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['recipename']" placeholder="请输入配方类型" disabled  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="当前存栏" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['cunlan']" placeholder="请输入当前存栏" style="width: 100%" disabled />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="配方牛头数(早)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['cowamountf']" placeholder="请输入配方牛头数(早)" style="width: 100%"  :disabled="man"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="配方牛头数(中)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['cowamountm']" placeholder="请输入配方牛头数(中)" style="width: 100%" :disabled="man"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="配方牛头数(晚)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['cowamountl']" placeholder="请输入配方牛头数(晚)" style="width: 100%" :disabled="man" />
            </a-form-item>
          </a-col>
          <a-col :span="24" v-show="showrecipeman">
            <a-form-item label="饲喂比例" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['feedprop']" placeholder="请输入饲喂比例"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="饲喂总量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['feedall']" placeholder="请输入饲喂总量" style="width: 100%" disabled  />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="早班计划量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['feedplanf']" placeholder="请输入早班计划量" style="width: 100%"  disabled />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="午班计划量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['feedplanm']" placeholder="请输入午班计划量" style="width: 100%"  disabled />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="晚班计划量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['feedplanl']" placeholder="请输入晚班计划量" style="width: 100%"  disabled />
            </a-form-item>
          </a-col>
          <a-col :span="24" v-show="showrecipeman">
            <a-form-item label="早班执行量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['feedworkf']" placeholder="请输入早班执行量" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24" v-show="showrecipeman">
            <a-form-item label="午班执行量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['feedworkm']" placeholder="请输入午班执行量" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24" v-show="showrecipeman">
            <a-form-item label="晚班执行量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['feedworkl']" placeholder="请输入晚班执行量" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24" v-show="showrecipeman">
            <a-form-item label="早班撒料时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-time placeholder="请选择早班撒料时间" v-decorator="['feedtimef']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24" v-show="showrecipeman">
            <a-form-item label="午班撒料时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-time placeholder="请选择午班撒料时间" v-decorator="['feedtimem']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24" v-show="showrecipeman">
            <a-form-item label="晚班撒料时间" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <j-time placeholder="请选择晚班撒料时间" v-decorator="['feedtimel']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="操作人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['admin']" placeholder="请输入操作人"  :disabled="man"></a-input>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'FdmTmrRecipemanForm',
    components: {
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        showrecipeman:false,
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/recipeman/fdmTmrRecipeman/add",
          edit: "/recipeman/fdmTmrRecipeman/edit",
          queryById: "/recipeman/fdmTmrRecipeman/queryById"
        },
        man:false,
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      edit (record) {
        const nowDate=new Date();
        let date = {
          year: nowDate.getFullYear(),
          month: nowDate.getMonth() + 1,
          date: nowDate.getDate()
        }
        const systemTime = Date.parse(date.year + '-' + date.month + '-' + date.date);

        if(systemTime>=Date.parse(record.selecttime)){
          console.log(7);
          this.man=true;
        }else{
          this.man=false;
        }
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'cowshename','recipename','cunlan','cowamountf','cowamountm','cowamountl','feedprop','feedall','feedplanf','feedplanm','feedplanl','feedworkf','feedworkm','feedworkl','feedtimef','feedtimem','feedtimel','admin'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'cowshename','recipename','cunlan','cowamountf','cowamountm','cowamountl','feedprop','feedall','feedplanf','feedplanm','feedplanl','feedworkf','feedworkm','feedworkl','feedtimef','feedtimem','feedtimel','admin'))
      },
    }
  }
</script>